package com.itwlj.controller.user;

import com.itwlj.context.BaseContext;
import com.itwlj.dto.AddressBookDTO;
import com.itwlj.entity.AddressBook;
import com.itwlj.result.Result;
import com.itwlj.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿
 */
@RestController
@RequestMapping("/address")
@Api(tags = "C端地址簿相关接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressService;

    /**
     * 新增地址
     *
     * @param addressBookDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result createAddress(@RequestBody AddressBookDTO addressBookDTO) {
        log.info("新增地址：{}", addressBookDTO);
        addressService.insert(addressBookDTO);
        return Result.success();
    }

    /**
     * 查询地址簿
     *
     * @return
     */
    @GetMapping
    @ApiOperation("查询地址簿")
    public Result<List<AddressBook>> getByUserId() {
        log.info("查询地址簿");
        List<AddressBook> addressBookList = addressService.getByUserId(BaseContext.getCurrentId());
        return Result.success(addressBookList);
    }

    /**
     * 设置默认地址
     *
     * @param id
     * @return
     */
    @PutMapping("/status")
    @ApiOperation("设置默认地址")
    public Result<String> setDefaultAddress(Long id) {
        log.info("设置默认地址：{}", id);
        addressService.setDefaultAddress(id);
        return Result.success();
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<String> deleteById(Long id) {
        log.info("根据id删除地址：{}", id);
        addressService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改地址
     *
     * @param addressBookDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址")
    public Result<String> update(@RequestBody AddressBookDTO addressBookDTO) {
        log.info("修改地址：{}", addressBookDTO);
        addressService.update(addressBookDTO);
        return Result.success();
    }
}
